﻿using Customer.API.Applcation.Command.MeetCommand;
using Customer.API.Dtos;
using Customer.Domain;
using Customer.Domain.Dto;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Customer.API.Controllers
{
    /// <summary>
    /// 应付管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class MeetController : ControllerBase
    {
        private readonly IMediator mediator;

        public MeetController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加应付
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMeet(ADDMeetDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.AddName = name;
                result.Data=await mediator.Send(new ADDMeetCommand() { addDTO= command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改应付
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdMeet(UpdMeetDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.AddName = name;
                result.Data = await mediator.Send(new UpdMeetCommand() { updDTO = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除应付应付
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelMeet(DelMeetCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.DelName = name;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }










    }
}
